<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<link rel="stylesheet" href="/css/dataTables.bootstrap.min.css">
<section class="content-header">
    <h1>
        生产定价
    </h1>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-body">
                <table id="table" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>产品名称</th>
                        <th>产品配方</th>
                        <th>配方成本</th>
                        <th>当前库存</th>
                        <th>当前定价</th>
                        <th>生产数量</th>
                        <th>定价</th>
                        <th>生产总监</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
                <button id="buy" class="btn btn-block btn-success">确认生产</button>
            </div>
        </div>
    </div>
</div>
</section>

<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/dataTables.bootstrap.min.js"></script>
<script>
var $select = $('<select name="product[member_id]">');
$.ajax('/api/member/list', {
    type: 'get',
    data: {
        position: 4,
    },
    dataType: 'json',
    headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
}).then(function (ret) {
    $select.append($('<option value="" disabled selected>请选择生产总监</option>'));
    console.log(ret.data.list)
    for (var i in ret.data.list) {
        $select.append($('<option>').attr('value', ret.data.list[i].id).text(ret.data.list[i].name));
    }


    $.fn.dataTable.ext.errMode = 'none';
    $.ajax('/api/product/list', {
        type: 'GET',
        data: {
            'status':2,
        },
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            var data = ret.data;
            $('#table').DataTable({
                "columnDefs": [
                    {
                        "targets": 1,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            var string = '';
                            for(var i in data) {
                                string += data[i].entry.name + '*' + data[i].num + ((data[i].entry.unit)?(data[i].entry.unit):'件') + ';';
                            }
                            return string;
                        }
                    },
                    {
                        "targets": 5,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            if(row.status==2) {
                                return '<input class="text-center" min="0" type="number" name="product[num]" lk-id="' + row.id + '" placeholder="请输入生产数量"/>';
                            } else {
                                return '-';
                            }
                        }
                    },
                    {
                        "targets": 6,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            if(row.status==2) {
                                return '<input class="text-center" min="0" type="number" name="product[price]" placeholder="请输入新的定价"/>';
                            } else {
                                return '-';
                            }
                        }
                    },
                    {
                        "targets": 7,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            if(row.status==2) {
                                return $select.prop("outerHTML");
                            } else {
                                return '产品设计未通过';
                            }
                        }
                    },
                ],
                data: data.list,
                columns: [
                    { data: 'name' },
                    { data: 'productMaterials' },
                    { data: 'material_cost' },
                    { data: 'userProduct.num' },
                    { data: 'userProduct.sale_price' },
                    { data: 'id' },
                    { data: 'id' },
                    // { data: 'office' }
                ],
                'paging'      : true,
                'ordering'    : true,
                'info'        : true,
                'autoWidth'   : false
            })
            $("table tr th").css({"text-align":"center"});
            $("table").css({"text-align":"center"});
        } else {
            alert(ret.message);
        }
    });
});


var ifRefresh = false;
$('#buy').click(function() {
    if(ifRefresh) {
        location.href = '/user/product';
        return;
    }

    var $nums = $('[name="product[num]"]');
    var $prices = $('[name="product[price]"]');
    var $member_ids = $('[name="product[member_id]"]');
    $prices.attr('disabled', 1)
    $member_ids.attr('disabled', 1)
    $nums.attr('disabled', 1).each(function(index, object) {
        var $num = $nums.eq(index);
        var $price = $prices.eq(index);
        var $member_id = $member_ids.eq(index);
        if($num.val()>0) {
            $.ajax('/api/product/product', {
                type: 'post',
                data: {
                    'product_id': $num.attr('lk-id'),
                    'num': $num.val(),
                    'price': $price.val(),
                    'member_id': $member_id.val(),
                },
                dataType: 'json',
                headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
            }).then(function(ret) {
                if(ret.state ==0) {
                    $num.parent().text('-');
                    $price.parent().text('-');
                    $member_id.parent().text('生产成功');
                } else {
                    $num.parent().text('-');
                    $price.parent().text('-');
                    $member_id.parent().text(ret.message);
                }
            });
        } else {
            $num.parent().text('-');
            $price.parent().text('-');
            $member_id.parent().text('-');
        }
    });
    $('#buy').text('生产结束，查看我的库存').removeClass('btn-primary');
    ifRefresh = true;
})
</script>